//
//  YCFriendNearbyViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/11.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  查找附近的人

#import "YCFriendNearbyViewController.h"
#import "YCFriendListCell.h"

@interface YCFriendNearbyViewController ()<UITableViewDataSource,UITableViewDelegate,BMKLocationServiceDelegate>

@property (readonly ,nonatomic) UIImageView *noImageView;
@property (readonly ,nonatomic) UILabel *noLabel;
@property (readonly ,nonatomic) UIView *headerView;
@property (readonly ,nonatomic) UILabel *allNumberLabel;
@property (readonly ,nonatomic) UITableView *tableView;
@property (readonly ,nonatomic) NSMutableArray *nextFriendsArray;
@property (strong   ,nonatomic) BMKLocationService *locationManager;
@property (readonly ,nonatomic) NSString *lat;
@property (readonly ,nonatomic) NSString *log;

@end

@implementation YCFriendNearbyViewController

@synthesize noImageView = _noImageView;
@synthesize noLabel = _noLabel;
@synthesize headerView = _headerView;
@synthesize allNumberLabel = _allNumberLabel;
@synthesize tableView = _tableView;
@synthesize nextFriendsArray = _nextFriendsArray;
@synthesize locationManager = _locationManager;

#pragma mark - 属性获取方法

// 定位
- (BMKLocationService *)locationManager
{
    if (!_locationManager) {
        //初始化BMKLocationService
        _locationManager = [[BMKLocationService alloc]init];
        _locationManager.delegate = self;
        _locationManager.distanceFilter = 10.0f;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    return _locationManager;
}

- (NSMutableArray *)nextFriendsArray
{
    if (!_nextFriendsArray)
    {
        _nextFriendsArray = [[NSMutableArray alloc] init];
    }
    
    return _nextFriendsArray;
}

- (UIView *)headerView
{
    if (!_headerView)
    {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 80)];
        _headerView.backgroundColor = [UIColor whiteColor];
    }
    
    return _headerView;
}

- (UIImageView *)noImageView
{
    if (!_noImageView)
    {
        _noImageView = [[UIImageView alloc] init];
        _noImageView.contentMode = UIViewContentModeCenter;
        _noImageView.backgroundColor = [UIColor clearColor];
        [_noImageView setImage:[UIImage imageNamed:@"nextUser_nomal"]];
        _noImageView.hidden = YES;
    }
    
    return _noImageView;
}

- (UILabel *)noLabel
{
    if (!_noLabel)
    {
        _noLabel = [[UILabel alloc] init];
        _noLabel.text = @"没有找到附近的人!";
        _noLabel.textColor = [UIColor grayColor];
        _noLabel.font = [UIFont systemFontOfSize:14];
        _noLabel.textAlignment = NSTextAlignmentCenter;
        _noLabel.hidden = YES;
    }
    
    return _noLabel;
}

- (UILabel *)allNumberLabel
{
    if (!_allNumberLabel)
    {
        _allNumberLabel = [[UILabel alloc] init];
        _allNumberLabel.textColor = [UIColor yc_colorWithUInt:0x555555];
        _allNumberLabel.font = [UIFont systemFontOfSize:16];
        _allNumberLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _allNumberLabel;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 80;
        _tableView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
        
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.3)];
        _tableView.tableFooterView.backgroundColor = [UIColor yc_colorWithUInt:0xc8c7cc];
    }
    
    return _tableView;
}

#pragma mark - 视图控制器生命周期

- (void)dealloc
{
    self.locationManager.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"附近的人", nil);
    [self yc_addBackBarButtonItem];
    [self yc_addRightBarButtonItemWithImageNamed:@"nav_Right_Dot" target:self action:@selector(didTouchUpInsideRightBarButtonItem:)];
    
    [self layout];
}

#pragma mark - 自定义的方法

- (void)layout
{
    
    if ([CLLocationManager locationServicesEnabled]) {
        //启动定位
        [self.locationManager startUserLocationService];
    }
    else {
        YCLog(@"请打开定位功能！");
    }
    
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    [self.headerView addSubview:self.allNumberLabel];
    [self.view addSubview:self.noImageView];
    [self.view addSubview:self.noLabel];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    [self.allNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.headerView);
        make.left.right.equalTo(self.headerView);
    }];
    
    [self.noImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view).offset(- 25);
        make.centerX.equalTo(self.view);
    }];
    
    [self.noLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(25);
    }];
}

#pragma mark - 点击事件
- (void)didTouchUpInsideRightBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    LXActionSheet *sheet = [[LXActionSheet alloc] initWithCancelButtonTitle:@"取消" otherButtonTitlesArray:@[@"清除位置信息并退出",@"查看全部",@"只看男生",@"只看女生"]];
    [sheet showInView:self.view mask:YES maskColor:[UIColor darkGrayColor] click:^(NSUInteger index, LXActionSheet *sheet) {
        switch (index) {
            case 1://清除位置信息并退出
            {
                YCLog(@"清除位置信息并退出");
            }
            break;
            case 2://查看全部
            {
                YCLog(@"查看全部");
            }
            break;
            case 3://只看男生
            {
                YCLog(@"只看男生");
            }
            break;
            case 4://只看女生
            {
                YCLog(@"只看女生");
            }
            break;
        
            default:
                break;
        }
        
        
    } cancel:nil];
}

#pragma mark - UITableView delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.nextFriendsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *iden = @"IDEN";
    
    YCFriendsListCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell)
    {
        cell = [[YCFriendsListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    
//    GOCustomerUser *user = self.nextFriendsArray[indexPath.row];
    
    [cell.headerImageView yc_setWebImageWithURLString:@"" holderSize:CGSizeMake(60, 60)];
    cell.nameLabel.text = @"张行";//user.cuNickName;
    cell.contentLabel.text = [NSString stringWithFormat:@"%@米",@"1000"];//user.cuDistance];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    GOCustomerUser *user = self.nextFriendsArray[indexPath.row];
//    GOShowOffModel *model = [[GOShowOffModel alloc] init];
//    model.userId = user.cuUserId;
//    model.headPhoto = user.cuHeadPhoto;
//    model.nickName = user.cuNickName;
//    GOOstentatiousForOtherViewController *vc = [[GOOstentatiousForOtherViewController alloc] initWithModel:model];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void)nextFriendsRequest
{
//    [self.tableView.go_loadingView startAnimating];
    
//    NSDictionary *ps = @{@"pageSize":@(1000),
//                         @"pageIndex":@(0),
//                         @"lat":_lat,
//                         @"lon":_log};
    
//    @go_weakify(self);
//    
//    GOWebServiceSuccessCallBack success = ^(AFHTTPRequestOperation *operation, GOWebServiceResponse *response) {
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self.tableView.go_loadingView stopAnimating];
//        });
//        
//        if(response.success)
//        {
//            NSArray *data = response.data;
//            
//            NSError *error;
//            
//            NSArray *objects = [MTLJSONAdapter modelsOfClass:[GOCustomerUser class] fromJSONArray:data error:&error];
//            
//            if(!error)
//            {
//                @go_strongify(self);
//                if(!self) return;
//                _allNumberLabel.text = [NSString stringWithFormat:@"找到%lu个用户",(unsigned long)objects.count];
//                
//                [self.nextFriendsArray addObjectsFromArray:objects];
//                
//                if (self.nextFriendsArray.count == 0)
//                {
//                    self.tableView.hidden = YES;
//                    self.noLabel.hidden = NO;
//                    self.noImageView.hidden = NO;
//                }
//                else
//                {
//                    self.tableView.hidden = NO;
//                    self.noLabel.hidden = YES;
//                    self.noImageView.hidden = YES;
//                }
//                
//                [self.tableView reloadData];
//                
//            }
//            else
//            {
//                
//            }
//        }
//    };
//    
//    GOWebServiceFailureCallBack failure = ^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    };
//    
//    [[GOWebService service] postAction:GOAPISearchUserSearchByLocation
//                                params:ps
//                               success:success
//                               failure:failure];

}

#pragma mark - BMKLocationServiceDelegate
// 定位失败
- (void)didFailToLocateUserWithError:(NSError *)error
{
    YCLog(@"定位失败");
}

// 定位成功
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    YCLog(@"当前定位成功，经度：%.6f 纬度:%.6f",userLocation.location.coordinate.longitude,userLocation.location.coordinate.latitude);
    
    _lat = [NSString stringWithFormat:@"%.6f",userLocation.location.coordinate.latitude];
    _log = [NSString stringWithFormat:@"%.6f",userLocation.location.coordinate.longitude];
    
    [self nextFriendsRequest];
    
    [self.locationManager stopUserLocationService];//停止定位
}



@end
